/**
 * Created by Administrator on 2016/1/7.
 */

var damoo;

$(function(){
    $(document).transition('options',{domCache:true});
    $('#dm-screen').css({
        height:$(window).height()-200
    });
    damoo = Damoo('dm-screen', 'dm-canvas', 14);
    damoo.start();
    damoo.hide();
    var connect = function(){
        var $this = $(this);
        var serverPath = $('#indexServer').val();
        var socket = io.connect(serverPath);
        socket.on("connect",function(){
            console.log("连接成功");
            socket.emit(command.sevent.login,{id:"index"});
        });
        //监听登录成功后跳转页面
        socket.on(command.sevent.login, function(msg){
            $('#message').html(msg.message);
            $(document).transition("to","signin.html");
        });
        socket.on(command.sevent.ctrl, function(msg){
            if(msg.type=='clipsControl'){
                var audioElement = $("#audioPlayer")[0];
                var clipsElement = $("#clipsPlayer")[0];
                if (!clipsElement.paused){
                    return;
                }
                $("#audioPlayer").removeAttr("loop");
                var tmpVol = audioElement.volume;
                audioElement.volume = 0;
                $("#clipsPlayer").attr('src',msg.url);
                var clilpsPlayerEndHandler = function(){
                    console.log(tmpVol);
                    audioElement.volume = tmpVol;
                };
                clipsElement.addEventListener("ended",clilpsPlayerEndHandler);
                clipsElement.play();
            }
            else if (msg.type=="mute"){
                $("#audioPlayer")[0].volume=0;
                $("#clipsPlayer")[0].volume=0;
            }
            else if (msg.type=="unMute"){
                $("#audioPlayer")[0].volume=1;
                $("#clipsPlayer")[0].volume=1;
            }
            else if(msg.type=='musicControl'){
                $("#audioPlayer").attr("src",msg.url);
                $("#audioPlayer")[0].play();
            }
            else if(msg.type=='transition'){
                $(document).transition("to",msg.url);
            }
            else if(msg.type=="videoAction"){
                $('#mv_title').html(msg.title);
                $('#mv_author').html(msg.author);

                swiperAnimateMany($('#mv_title'));
                swiperAnimateMany($('#mv_author'));
                video.src = msg.url;
                video.load();
            }
            else if(msg.type=="videoPlay"){
                video.play();
            }
            else if(msg.type=="videoStop"){
                video.pause();
            }
            else if(msg.type=="hideDamoo"){
                damoo.hide();
            }
            else if(msg.type=="showDamoo"){
                damoo.show();
            }else if(msg.type=="hideSnow"){
                removeSnow();
            }else if(msg.type=="yxyg"){
                var dept = msg.dept;
                var m = msg.m;
                var users = msg.users;
                $(document).transition("to","yxyg.html?dept="+dept+"&m="+m+"&users="+users);
            }else if(msg.type=="screenUp"){
                new TWEEN.Tween(movieScreen.position )
                    .to( {  y:11.05 }, Math.random() * 2000 + 4000 )
                    .easing( TWEEN.Easing.Exponential.InOut )
                    .start()
            }else if(msg.type=="screenDown"){
                new TWEEN.Tween(movieScreen.position )
                    .to( {  y:4 }, Math.random() * 2000 + 4000 )
                    .easing( TWEEN.Easing.Exponential.InOut )
                    .start()
            }else if(msg.type=="refresh"){
                location.href="http://localhost:4000";
            }
            else if(msg.type=="game"){
                var showId =msg.game;
                var url = msg.url;
                $(document).transition("to","game.html?id="+showId+"&url="+url);
            }else if(msg.type=="volUP"){
                var audioElement = $("#audioPlayer")[0];
                var clipsElement = $("#clipsPlayer")[0];
                audioElement.volume=audioElement.volume+0.2;
                clipsElement.volume=clipsElement.volume+0.2;
            }else if(msg.type=="volDown"){
                var audioElement = $("#audioPlayer")[0];
                var clipsElement = $("#clipsPlayer")[0];
                audioElement.volume=audioElement.volume-0.2;
                clipsElement.volume=clipsElement.volume-0.2;
            }
        });
    };
    connect();
    $("#connectBtn").click(connect);
});